<!--
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-04-07 20:18:30
 * @LastEditors: Sam
 * @LastEditTime: 2022-04-08 17:32:54
-->
<template>
    <div class="app-container">
        <el-card>
            <div slot="header" class="clearfix">
                <span>艺术家列表</span>
            </div>
             <div class="filter-container">
                    <el-input v-model="listQuery.name" placeholder="请输入姓名" clearable style="width: 200px;margin-right: 10px;margin-bottom:10px" class="filter-item" @keyup.enter.native="handleFilter" />

                <el-button v-waves  class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
                {{ '搜索' }}
                </el-button>
            <el-button v-waves :disabled="!checkPermission(['artist.create'])"  class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
                {{ "添加" }}
            </el-button>  



            </div>
             <el-table
                :header-cell-style="{background:'#eef1f6',color:'#606266'}"
                :data="list"
                border
                fit
                
                highlight-current-row
                style="width: 100%"
                size="mini"
                >  
                
                            <el-table-column  align="center" label="姓名">
                <template slot-scope="scope">
                    <span>{{ scope.row.name }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="头像">
                <template slot-scope="scope">
                    <span>{{ scope.row.avatar }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="头衔">
                <template slot-scope="scope">
                    <span>{{ scope.row.title }}</span>
                </template>
            </el-table-column>
            <el-table-column  align="center" label="简介">
                <template slot-scope="scope">
                    <span>{{ scope.row.biography }}</span>
                </template>
            </el-table-column>

                <el-table-column align="center" width="250" :label="'操作'">
                <template slot-scope="scope">
                    <el-button v-waves :disabled="!checkPermission(['artist.detail'])" size='mini'  type="info" icon="el-icon-info" @click="handleDetail(scope.$index, scope.row)">
                         {{ "详情" }}
                    </el-button>
                    <el-button v-waves v-permission="['artist.edit']"  type="primary"  size='mini' icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">
                         {{ "编辑" }}
                    </el-button>
                    <el-button v-waves v-permission="['artist.delete']"  type="danger" :destroy-on-close="true" size='mini' icon="el-icon-delete" @click="handleDelete(scope.$index, scope.row)">
                         {{ "删除" }}
                    </el-button>
                </template>
                </el-table-column>
            </el-table>

            <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
        </el-card>
            <el-dialog :title="'详情'" :visible.sync="detail.dialogVisible" v-if="detail.dialogVisible">
                    <detail :data="detail.data" />
            </el-dialog>
            <el-dialog :title="'编辑'" :visible.sync="edit.dialogVisible" @close="getList" :close-on-click-modal=false>
              <edit :item="edit" />
          </el-dialog>
            <el-dialog :title="'添加'" :visible.sync="create.dialogVisible" @close="getList"  :close-on-click-modal=false>
                <create :item="create" />
            </el-dialog>
    </div>
</template>
<script>
    import waves from '@/directive/waves' // waves directive
    import permission from '@/directive/permission/index.js' // 权限判断指令
    import checkPermission from '@/utils/permission' // 权限判断函数
    import { list, detail, del } from '@/api/artist' // api接口
    import { parseTime } from '@/utils' // 时间格式化函数
    import { getToken } from '@/utils/auth' // 获取token
    import Pagination from '@/components/Pagination' // 分页组件
    import Create from './components/create' // 添加组件  
    import Detail from '@/components/Detail/detail' // 详情组件 
    import Edit from './components/edit'  // 编辑组件  

    export default{
        name: 'artistIndex',
        components: { Pagination,Create,Detail,Edit }, // 引入组件
        filters: {
            parseTime // 时间过滤器
        },
        directives: { waves, permission }, // 引入指令
          data() {
            return {
             headerObj: {
                'Authorization': 'Bearer ' + getToken()
            },
            list: [], // 列表数据
            total: 0, // 总条数
            select_ids : [], // 选中的id集合
            listQuery: { //列表查询参数
                page: 1,
                limit: 10
            },
             create: { // 新增
                 dialogVisible: false
                },  
            detail: { // 详情
                   dialogVisible: false,
                   data: []
                },  
            edit: { // 编辑
                 dialogVisible: false,
                 id: ''
            },  

            }
        },
          created() {
            this.getList()
        },
        methods: {
            checkPermission, // 权限判断函数
            handleFilter() { // 查询
                this.listQuery.page = 1
                this.getList()
            },
            getList() { // 获取列表
                list(this.listQuery).then(response => {
                        this.list = response.data.data
                        this.total = response.data.total
                    })
                },
        handleCreate() { // 新增对话框显示
            this.create.dialogVisible = true
        },  
        handleDetail(index, row) { // 详情
            detail(row.id).then((res) => {
                    this.detail.dialogVisible = true
                    const data = res.data
                    this.detail.data = [
                    [                {
                    name:"姓名",
                    content:data.name,
                    type: "text"
                },
                {
                    name:"头像",
                    content:data.avatar,
                    type: "text"
                },
                {
                    name:"头衔",
                    content:data.title,
                    type: "text"
                },
                {
                    name:"简介",
                    content:data.biography,
                    type: "text"
                },
]
                    ]
                })
            },  
        handleEdit(index, row) { // 编辑对话框显示
            this.edit.dialogVisible = true
            this.edit.id = row.id
        },  
        handleDelete(index, row) { // 删除
            const thiz = this
            this.$confirm('确认要删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del(row.id).then(() => {
                this.$message({
                    message: '删除成功',
                    type: 'success',
                    duration: 1500,
                    onClose() {
                        thiz.list.splice(index, 1)
                    }
                })
                })
            }).catch(() => {

            })
        },   

            }
    }
</script>

